//package com.anti.modular.bus.busroom.service.impl;
//
//import cn.hutool.core.bean.BeanUtil;
//import cn.hutool.core.collection.CollectionUtil;
//import cn.hutool.core.util.ObjectUtil;
//import com.anti.commom.core.exception.BusinessException;
//import com.anti.commom.core.utils.StringUtils;
//import com.anti.core.context.login.LoginContextHolder;
//import com.anti.jdbc.DBUtils;
//import com.anti.jdbc.support.page.PageParam;
//import com.anti.jdbc.support.page.PageResult;
//import com.anti.modular.bus.basicinformation.entity.BasicInformation;
//import com.anti.modular.bus.basicinformation.service.BasicInformationService;
//import com.anti.modular.bus.busrealestate.result.BusRoomResult;
//import com.anti.modular.bus.busrealestate.result.RoomRentDesResult;
//import com.anti.modular.bus.busrealestate.result.RoomType;
//import com.anti.modular.bus.busrealestate.result.tree.BusRoomTreeRes;
//import com.anti.modular.bus.busrealestatefloor.entity.BusRealEstateFloor;
//import com.anti.modular.bus.busrealestatefloor.service.BusRealEstateFloorService;
//import com.anti.modular.bus.busroom.entity.BusRoom;
//import com.anti.modular.bus.busroom.enums.BusRoomExceptionEnum;
//import com.anti.modular.bus.busroom.param.BusRoomParam;
//import com.anti.modular.bus.busroom.service.BusRoomService;
//import com.anti.modular.bus.housebill.service.HouseBillService;
//import com.anti.modular.bus.roomdeposit.entity.RoomDeposit;
//import com.anti.modular.bus.roomdeposit.param.RoomDepositParam;
//import com.anti.modular.bus.roomdeposit.service.RoomDepositService;
//import com.anti.modular.bus.roomexpenses.entity.RoomExpenses;
//import com.anti.modular.bus.roomexpenses.service.RoomExpensesService;
//import com.anti.modular.bus.roomfurniture.entity.RoomFurniture;
//import com.anti.modular.bus.roomfurniture.service.RoomFurnitureService;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// * 房间表service接口实现类
// *
// * @author anti
// * @date 2021-04-16 19:14:02
// */
//@Service
//public class BusRoomServiceImplbak implements BusRoomService {
//    @Resource
//    DBUtils dbUtils;
//    @Resource
//    BusRealEstateFloorService busRealEstateFloorService;
//    @Resource
//    BasicInformationService basicInformationService;
//    @Resource
//    RoomDepositService roomDepositService;
//    @Resource
//    RoomExpensesService roomExpensesService;
//    @Resource
//    RoomFurnitureService roomFurnitureService;
//    @Resource
//    HouseBillService houseBillService;
//
//    @Override
//    public BusRoom getById(BusRoomParam busRoomParam){
//        String sql="select * from bus_room where id =? ";
//        return dbUtils.get(sql,BusRoom.class,busRoomParam.getId());
//    }
//
//    @Override
//    public List<BusRoomResult> getByIds(List<Long> ids, String searchType, String searchValue) {
//
//        StringBuilder sql=new StringBuilder("select a.*,(select count(*) from room_reservation where room_id = a.id and status=0) as reservation  from bus_room a where a.estateid in (:ids) and a.status=0");
//        HashMap<Object, Object> hashMap = CollectionUtil.newHashMap();
//        hashMap.put("ids",ids);
//        //'' 默認查詢  0 房間名查詢 1 狀態查詢 2 戶型查詢
//        if ("0".equals(searchType)&&StringUtils.isNotBlank(searchValue)){
//            sql.append(" and a.code = :code");
//            hashMap.put("code",searchValue);
//        }else if("1".equals(searchType)){
//            // 閑置
//            if ("2".equals(searchValue)){
//                sql.append(" and a.lease = :lease");
//                hashMap.put("lease",0);
//            }
//            // 已租
//            if ("3".equals(searchValue)){
//                sql.append(" and a.lease = :lease");
//                hashMap.put("lease",1);
//            }
//            //預定
//            if ("4".equals(searchValue)){
//                sql.append(" and a.id in (select room_id from room_reservation where status = 0)");
//            }
//        }else if ("2".equals(searchType)&&StringUtils.isNotBlank(searchValue)){
//            sql.append(" and a.room_type = :roomType");
//            hashMap.put("roomType",searchValue);
//        }
//        return dbUtils.query(sql.toString(),BusRoomResult.class,hashMap);
//    }
//
//    @Override
//    public List<BusRoom> getByFloorIds(List<Long> ids) {
//        StringBuilder sql=new StringBuilder("select * from bus_room where floorid in (:ids) and status=0");
//        HashMap<Object, Object> hashMap = CollectionUtil.newHashMap();
//        hashMap.put("ids",ids);
//        return dbUtils.query(sql.toString(),BusRoom.class,hashMap);
//    }
//
//    @Override
//    public List<BusRoomTreeRes> getBusRoomTreeResFloorIds(List<Long> ids) {
//        StringBuilder sql=new StringBuilder("select a.id,a.floorid,a.estateid,a.code,a.lease,(select count(*) from room_reservation where status=0 and room_id =a.id) as reservation from bus_room a where a.floorid in (:ids) and a.status=0");
//        HashMap<Object, Object> hashMap = CollectionUtil.newHashMap();
//        hashMap.put("ids",ids);
//        return dbUtils.query(sql.toString(),BusRoomTreeRes.class,hashMap);
//    }
//
//    @Override
//    public int getRoomCount(Long fcId, String type) {
//        ArrayList<Object> objects = CollectionUtil.newArrayList();
//        StringBuilder sb=new StringBuilder("select count(*) from bus_room where status=0 ");
//        if (type.equals("1")){
//            sb.append(" and estateid =? ");
//            objects.add(fcId);
//        }else {
//            sb.append(" and estateid in (select id from bus_real_estate where category=2 and status=0) ");
//        }
//
//        return dbUtils.getIntValue(sb.toString(),objects.toArray());
//    }
//
//    @Override
//    public int getRoomRentCount(Long fcId, String type) {
//        ArrayList<Object> objects = CollectionUtil.newArrayList();
//        StringBuilder sb=new StringBuilder("select count(*) from bus_room where status=0 and lease=1");
//        if (type.equals("1")){
//            sb.append(" and estateid =? ");
//            objects.add(fcId);
//        }else {
//            sb.append(" and estateid in (select id from bus_real_estate where category=2 and status=0) ");
//        }
//        return dbUtils.getIntValue(sb.toString(),objects.toArray());
//    }
//
//    @Override
//    public int getRoomRentCountByFloorId(Long floorId) {
//        String sql="select count(*) from bus_room where status=0 and lease=1 and floorid=?";
//        return dbUtils.getIntValue(sql,floorId);
//    }
//
//    /**
//     * 根據房產id 或房產類型查詢戶型
//     * @param stateId
//     * @param type
//     * @return
//     */
//    @Override
//    public List<RoomType> getRoomTypeByStateId(Long stateId, String type) {
//        if (type.equals("1")){
//            String sql="select room_type,room_type_name from bus_room where status=0 and estateid =? group by  room_type,room_type_name";
//            return dbUtils.query(sql, RoomType.class,stateId);
//        }else {
//            String sql="select room_type,room_type_name from bus_room where status=0 and estateid in (select id from bus_real_estate where status=0 and category=2) group by  room_type,room_type_name";
//            return dbUtils.query(sql, RoomType.class);
//        }
//    }
//
//    @Override
//    public RoomRentDesResult getAllRentDes(Long stateId,String type) {
//        if ("1".equals(type)){
//            String sql="select sum(rent) as allrent, sum(deposit) as alldeposit from bus_room where estateid =? and status =0";
//            return dbUtils.getFirst(sql, RoomRentDesResult.class,stateId);
//        }
//        String sql="select sum(rent) as allrent, sum(deposit) as alldeposit from bus_room where status =0 and estateid in (select id from bus_real_estate where status=0 and category=2)";
//        return dbUtils.getFirst(sql, RoomRentDesResult.class);
//    }
//
//    @Override
//    public List<BusRoom> getRoomByStateId(Long stateId, String type) {
//        if (type.equals("1")){
//            String sql="select * from bus_room where status=0 and estateid =? group by  room_type,room_type_name";
//            return dbUtils.query(sql, BusRoom.class,stateId);
//        }else {
//            String sql="select *  from bus_room where status=0 and estateid in (select id from bus_real_estate where status=0 and category=2) group by  room_type,room_type_name";
//            return dbUtils.query(sql, BusRoom.class);
//        }
//    }
//
//    @Override
//    public List<BusRoomTreeRes> getBusRoomTreeResByStateId() {
//        String sql="select a.id,a.floorid,a.estateid,a.code,a.lease,(select count(*) from room_reservation where status=0 and room_id =a.id) as reservation from bus_room a where a.status=0 and a.estateid in (select id from bus_real_estate where status=0 and category=2)";
//        return dbUtils.query(sql, BusRoomTreeRes.class);
//    }
//
//    @Override
//    public PageResult page(PageParam pageParam,BusRoomParam busRoomParam) {
//        StringBuilder sb=new StringBuilder("select * from bus_room where 1=1 ");
//        ArrayList<Object> param = CollectionUtil.newArrayList();
//        if (ObjectUtil.isNotNull(busRoomParam)) {
//            // 根据房间编号 查询
//            if (ObjectUtil.isNotEmpty(busRoomParam.getCode())) {
//               sb.append(" and code = ?");
//               param.add(busRoomParam.getCode());
//            }
//        }
//        return dbUtils.query(pageParam,sb.toString(),param.toArray());
//    }
//
//    @Override
//    public List<BusRoom> list(BusRoomParam busRoomParam) {
//        String sql="";
//        return dbUtils.query(sql,BusRoom.class);
//    }
//
//    @Override
//    public void add(BusRoomParam busRoomParam) {
//        checkParam(busRoomParam,false);
//        BusRoom busRoom = new BusRoom();
//        BeanUtil.copyProperties(busRoomParam, busRoom);
//        Long sysLoginUserId = LoginContextHolder.me().getSysLoginUserId();
//        Date date = new Date();
//        busRoom.setCreateTime(date);
//        busRoom.setCreateUser(sysLoginUserId);
//        busRoom.setLease(0);
//        busRoom.setStatus(0);
//        String strKey = dbUtils.insertKey(busRoom).toString();
//        long key = Long.parseLong(strKey);
//
//        /**
//         * 押金列表
//         */
//        List<RoomDeposit> yjData = busRoomParam.getYjData();
//        yjData.forEach(yj->{
//            yj.setRoomid(key);
//            yj.setCreateTime(date);
//            yj.setCreateUser(sysLoginUserId);
//            yj.setStatus(0);
//            roomDepositService.addEntity(yj);
//        });
//        /**
//         * 費用列表
//         */
//        List<RoomExpenses> fyData = busRoomParam.getFyData();
//        // 租金水电是否分开收取（1 是 0不是）
//        int hydropower = busRoomParam.getHydropower();
//        fyData.forEach(fy->{
//            fy.setRoomid(key);
//            fy.setCreateTime(date);
//            if (hydropower == 0){
//                fy.setYdzd(1);
//            }
//            fy.setCreateUser(sysLoginUserId);
//            fy.setStatus(0);
//            roomExpensesService.addEntity(fy);
//        });
//        /**
//         * 家私列表
//         */
//        List<RoomFurniture> jsData = busRoomParam.getJsData();
//        jsData.forEach(js->{
//            js.setRoomid(key);
//            js.setCreateTime(date);
//            js.setCreateUser(sysLoginUserId);
//            js.setStatus(0);
//            roomFurnitureService.addEntity(js);
//        });
//    }
//
//
//    public void delete(BusRoomParam busRoomParam) {
//        int rentRoomNumByRoomId = this.getRentRoomNumByRoomId(busRoomParam.getId());
//        if (rentRoomNumByRoomId>0){
//            throw new BusinessException(0,"該房間有在租租約，請先退租");
//        }
//        // 预约
//        String sql="update  bus_room set status=2,update_time=?,update_user=? where id =? ";
//        dbUtils.execSql(sql,new Date(),LoginContextHolder.me().getSysLoginUserId(),busRoomParam.getId());
//        roomDepositService.deleteYjByRoomId(busRoomParam.getId());
//        roomExpensesService.deleteFyByRoomId(busRoomParam.getId());
//        roomFurnitureService.deleteJsByRoomId(busRoomParam.getId());
//    }
//
//    public void edit(BusRoomParam busRoomParam) {
//        checkParam(busRoomParam,true);
//        BusRoom busRoom = this.queryBusRoom(busRoomParam);
//        BeanUtil.copyProperties(busRoomParam, busRoom);
//        Long sysLoginUserId = LoginContextHolder.me().getSysLoginUserId();
//        Date date = new Date();
//        busRoom.setUpdateTime(date);
//        busRoom.setUpdateUser(sysLoginUserId);
//        dbUtils.update(busRoom);
//        Long id = busRoom.getId();
//        /**
//         * 押金列表
//         */
//        List<RoomDeposit> yjData = busRoomParam.getYjData();
//        //先删除id不在的 并且删除对账单详情中的
//        List<Long> collectYjIds = yjData.stream().filter(it -> ObjectUtil.isNotEmpty(it.getId())).map(it -> it.getId()).collect(Collectors.toList());
//        roomDepositService.deleteYjByRoomIds(collectYjIds);
//        //在更新修改的
//        yjData.forEach(it->{
//            RoomDepositParam roomDepositParam=null;
//            if (ObjectUtil.isNotEmpty(it.getId())){
//                roomDepositParam=new RoomDepositParam();
//                roomDepositParam.setAmount(it.getAmount());
//                roomDepositParam.setQuantity(it.getQuantity());
//                roomDepositService.edit(roomDepositParam);
//            }
//        });
//        //最后保存添加的
//        yjData.forEach(yj->{
//            if (!ObjectUtil.isNotEmpty(yj.getId())){
//                yj.setId(null);
//                yj.setRoomid(id);
//                yj.setCreateTime(date);
//                yj.setCreateUser(sysLoginUserId);
//                yj.setStatus(0);
//                roomDepositService.addEntity(yj);
//            }
//        });
//        /**
//         * 費用列表
//         */
//        roomExpensesService.deleteFyByRoomId(id);
//        List<RoomExpenses> fyData = busRoomParam.getFyData();
//        // 租金水电是否分开收取（1 是 0不是）
//        int hydropower = busRoomParam.getHydropower();
//        fyData.forEach(fy->{
//            fy.setId(null);
//            fy.setRoomid(id);
//            fy.setCreateTime(date);
//            if (hydropower == 0){
//                fy.setYdzd(1);
//            }
//            fy.setCreateUser(sysLoginUserId);
//            fy.setStatus(0);
//            roomExpensesService.addEntity(fy);
//        });
//        /**
//         * 家私列表
//         */
//        roomFurnitureService.deleteJsByRoomId(id);
//        List<RoomFurniture> jsData = busRoomParam.getJsData();
//        jsData.forEach(js->{
//            js.setId(null);
//            js.setRoomid(id);
//            js.setCreateTime(date);
//            js.setCreateUser(sysLoginUserId);
//            js.setStatus(0);
//            roomFurnitureService.addEntity(js);
//        });
//        String a="dd";
//        Long.parseLong(a);
//    }
//
//    @Override
//    public BusRoom detail(BusRoomParam busRoomParam) {
//        return this.queryBusRoom(busRoomParam);
//    }
//
//    @Override
//    public int getRentRoomNumByFzId(Long fcId) {
//        String sql="select count(*) from bus_room where status=0 and lease=1 and  estateid =?";
//        return dbUtils.getIntValue(sql,fcId);
//    }
//
//    @Override
//    public int getRentRoomNumByFloorId(Long fcId) {
//        String sql="select count(*) from bus_room where status=0 and lease=1 and  floorid =?";
//        return dbUtils.getIntValue(sql,fcId);
//    }
//
//    @Override
//    public int getRentRoomNumByRoomId(Long fcId) {
//        String sql="select count(*) from bus_room where status=0 and lease=1 and  id =?";
//        return dbUtils.getIntValue(sql,fcId);
//    }
//
//    @Override
//    public void deleteByFcId(Long id) {
//        String sql="update  bus_room set status=2,update_time=?,update_user=? where estateid =? ";
//        dbUtils.execSql(sql,new Date(), LoginContextHolder.me().getSysLoginUserId(),id);
//    }
//
//    @Override
//    public void deleteRoomByStateId(Long id) {
//        String selectSql="select * from bus_room where status=0 and estateid =?";
//        List<BusRoom> query = dbUtils.query(selectSql, BusRoom.class, id);
//        if (query.size()>0){
//            String sql="update  bus_room set status=2,update_time=?,update_user=? where estateid =?  and status=0";
//            dbUtils.execSql(sql,new Date(), LoginContextHolder.me().getSysLoginUserId(),id);
//            query.forEach(item->{
//                roomDepositService.deleteYjByRoomId(item.getId());
//                roomExpensesService.deleteFyByRoomId(item.getId());
//                roomFurnitureService.deleteJsByRoomId(item.getId());
//            });
//        }
//    }
//
//    @Override
//    public void deleteRoomByFloorId(Long stateId, Long floorId) {
//        String selectSql="select * from bus_room where status=0 and estateid =? and floorid = ?";
//        List<BusRoom> query = dbUtils.query(selectSql, BusRoom.class, stateId, floorId);
//        if (query.size()>0){
//            String sql="update  bus_room set status=2,update_time=?,update_user=? where estateid =? and floorid = ? and status=0";
//            dbUtils.execSql(sql,new Date(), LoginContextHolder.me().getSysLoginUserId(),stateId,floorId);
//            query.forEach(item->{
//                roomDepositService.deleteYjByRoomId(item.getId());
//                roomExpensesService.deleteFyByRoomId(item.getId());
//                roomFurnitureService.deleteJsByRoomId(item.getId());
//            });
//        }
//    }
//
//    /**
//     * 获取房间表
//     *
//     * @author anti
//     * @date 2021-04-16 19:14:02
//     */
//    private BusRoom queryBusRoom(BusRoomParam busRoomParam) {
//        BusRoom busRoom = this.getById(busRoomParam);
//        if (ObjectUtil.isNull(busRoom)) {
//            throw new BusinessException(BusRoomExceptionEnum.NOT_EXIST);
//        }
//        return busRoom;
//    }
//
//
//    private void checkParam(BusRoomParam busRoomParam, boolean isExcludeSelf){
//        Long id = busRoomParam.getId();
//        Long estateid = busRoomParam.getEstateid();
//        Long floorid = busRoomParam.getFloorid();
//        String code = busRoomParam.getCode();
//        int roomType = busRoomParam.getRoomType();
//        //构建带name查询条件
//        StringBuilder nameSb=new StringBuilder("select count(*) from bus_room where status =0 and estateid =? and code =? ");
//        ArrayList<Object> nameParams = CollectionUtil.newArrayList();
//        nameParams.add(estateid);
//        nameParams.add(code);
//        if (ObjectUtil.isNotEmpty(floorid)){
//            nameSb.append(" and floorid = ? ");
//            nameParams.add(floorid);
//        }
//        //如果排除自己，则增加查询条件主键id不等于本条id
//        if (isExcludeSelf) {
//            nameSb.append(" and id <> ?");
//            nameParams.add(id);
//        }
//
//        //查询重复记录的数量
//        int countByName = dbUtils.getIntValue(nameSb.toString(),nameParams.toArray());
//
//        //如果存在重复的记录，抛出异常，直接返回前端
//        if (countByName >= 1) {
//            throw new BusinessException(0,"該房間編號已存在");
//        }
//        // 裝配參數
//        if (ObjectUtil.isNotEmpty(floorid)){
//            //樓層
//            BusRealEstateFloor floor = busRealEstateFloorService.getFloorById(floorid);
//            busRoomParam.setFloor(floor.getName());
//        }
//        //戶型
//        if (roomType==0){
//            busRoomParam.setRoomTypeName("無戶型");
//        }else {
//            BasicInformation information = basicInformationService.getBasicInformationById(busRoomParam.getRoomType());
//            if (ObjectUtil.isNotEmpty(information)){
//                busRoomParam.setRoomTypeName(information.getName());
//            }
//        }
//        String zqName = getZqName(busRoomParam);
//        busRoomParam.setReceiptcycle(zqName);
//    }
//
//   public String getZqName(BusRoomParam busRoomParam){
//        // 依据（字典 1日 2月 3年 4一次付清）
//       int receiptyj = busRoomParam.getReceiptyj();
//        // 付 （字典 付1-付18）
//       int receiptfu = busRoomParam.getReceiptfu();
//       // 押 （字典 押1-押18-自定义19）
//       int receiptya = busRoomParam.getReceiptya();
//       if (receiptyj == 4){
//           return "一次付清";
//       }
//       String dw="月";
//       if (receiptyj == 1){
//           dw="日";
//       }else if (receiptyj == 2){
//           dw="月";
//       }else if (receiptyj == 3){
//           dw="年";
//       }
//
//       if (receiptya==19){
//           return "付"+receiptfu+dw+" 押金自定義";
//       }
//       return "付"+receiptfu+dw+" 押"+receiptya+dw;
//    }
//}
